package com.zzvcom.webapp.action.web;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzvcom.persist.model.admin.Config;
import com.zzvcom.persist.model.admin.Meta;
import com.zzvcom.persist.model.admin.Product;
import com.zzvcom.persist.model.admin.ProductDes;
import com.zzvcom.service.admin.ConfigManager;
import com.zzvcom.service.admin.MetaManager;
import com.zzvcom.service.admin.ProductClickManager;
import com.zzvcom.service.admin.ProductDesManager;
import com.zzvcom.service.admin.ProductManager;
import com.zzvcom.template.ResultPage;
import com.zzvcom.utils.ObjectUtil;

@Controller
@Scope("prototype")
// 声明此类为控制层的类,且为prototype/singleton模式调用
public class ProductViewAction
{
    private ProductManager pm;

    private ProductDesManager pdm;

    private MetaManager mm;

    private ProductClickManager pcm;

    private ConfigManager cm;

    private ResultPage resultPage;

    private Product product;

    private String params;

    private String page = "1";

    public String getParams()
    {
        return params;
    }

    public void setParams(String params)
    {
        this.params = params;
    }

    /**
     * default display all the product
     * 
     * @return
     */

    private DetachedCriteria helper()
    {
        DetachedCriteria dc = DetachedCriteria.forClass(Product.class);
        dc.addOrder(Order.desc("addtime"));
        return dc;
    }

    /**
     * all product list
     * 
     * @return
     */
    public String execute()
    {
        if (resultPage == null)
        {
            resultPage = new ResultPage();
            resultPage.setPageSize(12);
        }
        DetachedCriteria dc = helper();
        resultPage.setDetachedCriteria(dc);
        pm.getResultPageByDetachedCriteria(resultPage);
        return "product";
    }

    /**
     * product with condition params must split with '-' for this must have less
     * then two params
     * 
     * @return
     */
    public String c()
    {
        if (StringUtils.isBlank(params))
        {
            return execute();
        }
        if (resultPage == null)
        {
            resultPage = new ResultPage();
            resultPage.setPageSize(12);
        }
        if (StringUtils.isBlank(page))
        {
            setPage("0");
        }
        resultPage.setCurrentPage(Integer.valueOf(page));
        DetachedCriteria dc = helper();
        String[] paramArray = null;
        if (!params.equals("*"))
        {
            // System.out.println(params);
            for (String pa : params.split("--"))
            {
                paramArray = pa.split("-");
                dc.add(Restrictions.eq(paramArray[0], paramArray[1]));
            }
        }
        dc.addOrder(Order.desc("addtime"));
        resultPage.setDetachedCriteria(dc);
        pm.getResultPageByDetachedCriteria(resultPage);
        return "product";
    }

    /**
     * a product list
     * 
     * @return
     */

    private ProductDes des;

    private List<Meta> metas;

    private List<Product> similar;

    public String a()
    {
        product = pm.get(params);
        pcm.updateByHQL("update ProductClick set click=click+1 where id=?",product.getId());
        des = (ProductDes) pdm.queryUniqueByHQL(
                "from ProductDes where productid=?", product.getId());
        metas = mm.queryByHQL("from Meta where productid=?", product.getId());

        similar = pm
                .queryByHQL(
                        "from Product where ( ( quality=? and size=? )  or ( retailprice>? and retailprice<? ) ) and id <> ? order by click desc",
                        0, 4, product.getQuality(), product.getSize(), product
                                .getRetailprice() - 10, product
                                .getRetailprice() + 10,product.getId());

        return "aproduct";
    }

    public String hot()
    {
        resultPage = new ResultPage();
        resultPage.setPageSize(12);
        DetachedCriteria dc = helper();
        if (StringUtils.isNotBlank(params))
        {
            resultPage.setCurrentPage(Integer.valueOf(params));
        }
        dc.add(Restrictions.eq("hot", "0"));
        dc.addOrder(Order.desc("addtime"));
        resultPage.setDetachedCriteria(dc);
        pm.getResultPageByDetachedCriteria(resultPage);
        return "hotproduct";
    }

    private void t()
    {
        if (resultPage == null)
        {
            resultPage = new ResultPage();
            resultPage.setPageSize(10);
        }
        DetachedCriteria dc = helper();
        resultPage.setDetachedCriteria(dc);
        Config config = cm.getConfig();
        Product tp = null;
        pm.getResultPageByDetachedCriteria(resultPage);
        for (int i = 1; i <= resultPage.getTotalPage(); i++)
        {
            dc = helper();
            resultPage.setDetachedCriteria(dc);
            resultPage.setCurrentPage(i);
            resultPage.getResult().clear();
            resultPage = pm.getResultPageByDetachedCriteria(resultPage);
            for (int j = 0; j < resultPage.getResult().size(); j++)
            {
                tp = (Product) resultPage.getResult().get(j);
                tp.setNo(config.getNoprefix()
                        + ObjectUtil.fillzero5((i - 1) * 10 + j + 1));
                pm.save(tp);
            }
        }
        config.setNo(resultPage.getTotalRecord());
        cm.save(config);

    }

    @Autowired
    public void setPm(ProductManager pm)
    {
        this.pm = pm;
    }

    public ResultPage getResultPage()
    {
        return resultPage;
    }

    public void setResultPage(ResultPage resultPage)
    {
        this.resultPage = resultPage;
    }

    public Product getProduct()
    {
        return product;
    }

    public void setProduct(Product product)
    {
        this.product = product;
    }

    public ProductManager getPm()
    {
        return pm;
    }

    public static void main(String arg[])
    {
        String params = "name-c";
        String[] pa = params.split("--");
        System.out.println(pa[0].split("-")[1]);
    }

    public String getPage()
    {
        return page;
    }

    public void setPage(String page)
    {
        this.page = page;
    }

    public ConfigManager getCm()
    {
        return cm;
    }

    @Autowired
    public void setCm(ConfigManager cm)
    {
        this.cm = cm;
    }

    public ProductDesManager getPdm()
    {
        return pdm;
    }

    @Autowired
    public void setPdm(ProductDesManager pdm)
    {
        this.pdm = pdm;
    }

    public MetaManager getMm()
    {
        return mm;
    }

    @Autowired
    public void setMm(MetaManager mm)
    {
        this.mm = mm;
    }

    public ProductDes getDes()
    {
        return des;
    }

    public void setDes(ProductDes des)
    {
        this.des = des;
    }

    public List<Meta> getMetas()
    {
        return metas;
    }

    public void setMetas(List<Meta> metas)
    {
        this.metas = metas;
    }

    public ProductClickManager getPcm()
    {
        return pcm;
    }

    @Autowired
    public void setPcm(ProductClickManager pcm)
    {
        this.pcm = pcm;
    }

    public List<Product> getSimilar()
    {
        return similar;
    }

    public void setSimilar(List<Product> similar)
    {
        this.similar = similar;
    }

}
